<template>
  <div class="login">
    <!-- <he-he ref="hehe"></he-he> -->

    <div class="login_box">
      <div class="logo_box">
        <img src="../assets/123.png" alt="" />
      </div>

      <div class="form_box" ref="mydiv">
        <!-- model: 表单数据对象 -->
        <!-- rules: 表单验证规则对象 -->
        <el-form
          ref="form"
          class="demo-ruleForm"
          :model="form.data"
          :rules="form.rules"
        >
          <!-- prop: 表单数据的键名 -->
          <el-form-item prop="username">
            <el-input
              type="text"
              prefix-icon="el-icon-user-solid"
              placeholder="请输入用户名"
              v-model="form.data.username"
              @keyup.enter.native="submit"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <!-- 事件修饰符 .native 可以监听自定义组件的原生事件，原理：监听在组件模板的根元素中，子元素可以通过事件冒泡传递上来 -->
            <el-input
              type="password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              v-model="form.data.password"
              @keyup.enter.native="submit"
            ></el-input>
          </el-form-item>
          <el-form-item class="btn_box">
            <el-button type="primary" @click="submit">登录</el-button>
            <el-button type="info" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import HeHe from '@/components/HeHe.vue';
export default {
  name: 'Login',
  // components: { HeHe },
  data() {
    return {
      form: {
        data: {
          username: '',
          password: '',
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            {
              min: 2,
              max: 16,
              message: '用户名长度在2~16个字符之间',
              trigger: 'blur',
            },
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            {
              min: 6,
              max: 16,
              message: '密码长度在6~16个字符之间',
              trigger: 'blur',
            },
          ],
        },
      },
    };
  },
  methods: {
    reset() {
      this.$refs.form.resetFields();
    },
    submit() {
      // 在提交之前，要先判断一下表单数据是否合法
      this.$refs.form
        .validate()
        .then(() => {
          return this.__apis.login(this.form.data);
        })
        .then((res) => {
          if (res.data.meta.status === 200) {
            localStorage.setItem('token', res.data.data.token);
            this.$router.push('/');
          } else {
            console.log(res.data.meta.msg);
          }
        });
    },
  },
  mounted() {
    // 如果ref引用的是一个原始HTML元素，那么拿到的就是js原始的dom对象
    // console.log(this.$refs.mydiv);
    // 如果ref引用的是一个Vue组件元素，那么拿到的就是该Vue组件的实例对象
    // console.log(this.$refs.hehe.sayHi());
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #2b4b6b;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 450px;
    height: 304px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: #fff 0 0 5px 1px;
    position: relative;
    .logo_box {
      position: absolute;
      transform: translate(-50%, -50%);
      left: 50%;
      width: 200px;
      height: 200px;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 50%;
      box-shadow: #ddd 0 0 10px 1px;
      overflow: hidden;
      padding: 10px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 100%;
        background-color: #efefef;
      }
    }
    .form_box {
      padding: 20px;
      width: 100%;
      position: absolute;
      box-sizing: border-box;
      bottom: 0;
      .btn_box {
        display: flex;
        justify-content: end;
        margin-bottom: 0;
        float: right;
      }
    }
  }
}
</style>
